package com.zhongxi.guoyuan.adapter.shopCart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.rewardcoin.ShopCartResult;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/22.
 */

public class AttrCartAdapter extends BaseAdapter {

    private Context context;
    private JSONObject attrObj;
    private List<String> key;
    private ShopCartResult.ToDoor list;
    private List<String> value;

    public AttrCartAdapter(Context context, JSONObject attrObj, List<String> key, List<String> value, ShopCartResult.ToDoor list) {
        this.context = context;
        this.attrObj = attrObj;
        this.key = key;
        this.value = value;
        this.list = list;
    }

    @Override
    public int getCount() {
        return key.size();
    }

    @Override
    public Object getItem(int position) {
        return attrObj;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_attr, parent, false);
                holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
                holder.gvList = (NoScrollGridView) convertView.findViewById(R.id.gvList);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (null != attrObj.getString(key.get(position)) && !"".equals(attrObj.getString(key.get(position)))) {
                holder.tvTitle.setText(key.get(position));

                final List<String> values = new ArrayList<>();
                JSONArray attrVal = attrObj.getJSONArray(key.get(position));
                int pos = 0;
                for(int i=0;i<attrVal.length();i++){
                    values.add(attrVal.get(i).toString());
                    if(attrVal.get(i).toString().equals(value.get(position))){
                        pos = i;
                    }
                }
                final AttrValueCartAdapter adapter = new AttrValueCartAdapter(context, values, pos);
                holder.gvList.setAdapter(adapter);
                //单选框
                holder.gvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                        /*for(int i=0;i<values.size();i++){
                            if(key.get(position).equals(values.get(i).getName())) {
                                String s = values.get(pos);
                                list.getAttr().get(i).setValue(s);
                            }
                        }*/
                        for(int i=0;i<key.size();i++){
                            if(key.get(position).equals(list.getAttr().get(i).getName())){
                                list.getAttr().get(i).setValue(values.get(pos));
                            }
                        }
                        adapter.setSelect(pos);
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }



    class ViewHolder{
        private TextView tvTitle;
        private NoScrollGridView gvList;
    }
}
